﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using OgilvyOne.Model;
using OgilvyOne.BLL;

public partial class admin_infomation_info_detail : AdminBasePage
{
    private static readonly string TAG_LAYERIDS = OgilvyOne.Utility.ConfigUtil.GetStringValue("ArticleTagLayerids");
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.infoname.Text = "新增内容";
            this.headers.Text = new ColumnBll().LayeridColumnConNaiv(Layerid);

            int columnid = int.Parse(Request.Params["columnid"]);
            ColumnModel column = new ColumnBll().GetColumn(columnid);

            this.isstaticpub.Value = column.Isstaticpub.ToString();
            

            //初始模板
            ModelManageBLL mmb = new ModelManageBLL();
            DataSet temdata = mmb.GetModel("");
            for (int i = 0; i < temdata.Tables.Count; i++)
            {
                DataTable table = temdata.Tables[i];
                for (int j = 0; j < table.Rows.Count; j++)
                {
                    DataRow row = table.Rows[j];

                    this.templateId.Items.Add(new ListItem(row["tem_name"].ToString(), row["tem_id"].ToString()));
                    
                }
            }
            //初始专家
            IList expertdatalist = new HealthExpertBll().GetHealthExpertList(null);
            for (int i = 0; i < expertdatalist.Count; i++)
            {
                HealthExpertModel hem = (HealthExpertModel)expertdatalist[i];
                expertlist.Items.Add(new ListItem(hem.ExpertName, hem.ExpertId.ToString()));
            }

            if (isExist(Layerid, TAG_LAYERIDS))
            {
                this.contag.Visible = true;
            }

            this.sub3.Visible = false;
            this.sub4.Visible = false;
            this.sub5.Visible = false;
            if (Request.Params["id"] != null && Request.Params["id"] != "")
            {//修改
                this.infoname.Text = "修改内容";
                CmsArticleModel cm = new CmsArticleBll().GetCmsArticleModel(int.Parse(Request.Params["id"]));
                this.title.Text = cm.Title;
                this.summary.Text = cm.Summary;
                this.content.Text = cm.Content;
                this.indextop.Text = cm.Indextop;
                this.sorttop.Text = cm.Sorttop;
                this.titleImg.Value = cm.Titleimg;
                this.state.Value = cm.Docstate;
                this.aid.Value = cm.Articleid.ToString();
                this.templateId.Text = cm.Templateid.ToString();
                this.isstaticpub.Value = cm.Isstaticpub;
                this.articletype.Text = cm.Articletype;
                this.source.Text = cm.Source;
                this.sourceurl.Text = cm.Sourceurl;
                this.linkpath.Text = cm.Linkpath;
                this.author.Text = cm.Author;

                if (cm.ExpertId > 0)
                {
                    this.expertid.Value = cm.ExpertId.ToString();
                    this.expertlist.Text = cm.ExpertId.ToString();
                }
                //需加对应TAG NAME
                this.tag.Value = cm.Tag;
                if (cm.Tag != null && cm.Tag != "")
                {
                    IList taglist = new CmsArticleTagBll().GetTagList(cm.Tag);
                    for (int i = 0; i < taglist.Count; i++)
                    {
                        CmsArticleTagModel catm = (CmsArticleTagModel)taglist[i];
                        this.tagname.Text += catm.TagName + ",";
                    }
                }
                this.sub3.Visible = true;

                if (column.Isauditing == 1)
                {
                    if(cm.Docstate!="2") sub3.Visible = false;
                    if (this.PopedomValidateHtml(Request.Params["layerid"], AUDIT))
                    {
                        if (cm.Docstate == "0")
                        {//正稿可以审核
                            this.sub4.Visible = true;
                            this.sub5.Visible = true;
                        }
                    }
                    if(Request.Params["isaudit"]!=null && Request.Params["isaudit"]=="1")
                    {//审核文章
                        this.sub1.Visible = false;
                        this.sub2.Visible = false;
                    }
                }
            }

            
            
        }
    }

    protected void Audi_click(object sender, EventArgs e)
    {
        this.PopedomValidate(Request.Params["layerid"], AUDIT);
        new CmsArticleBll().Audit(int.Parse(this.aid.Value), this.state.Value, GetSessionUsersModel().Username);
        Response.Redirect("info_list.aspx?columnid=" + Request.Params["columnid"] + "&layerid=" + Request.Params["layerid"], true);
    }

    protected void Sub_click(object sender, EventArgs e)
    {
        int aid = 0;
        if (this.aid.Value!=null && this.aid.Value != "")
        {
            aid = int.Parse(this.aid.Value);
        }

        CmsArticleModel cm = new CmsArticleModel();
        cm.Title = this.title.Text;
        cm.Summary = this.summary.Text;
        cm.Content = this.content.Text;
        cm.Indextop = this.indextop.Text;
        cm.Sorttop = this.sorttop.Text;
        //cm.Titleimg = this.titleImg.Value; 相关图片 维护
        cm.Docstate = this.state.Value;
        cm.Templateid = int.Parse(this.templateId.Text);
        cm.Isstaticpub = this.isstaticpub.Value;

        cm.Articletype = this.articletype.Text;
        cm.Source = this.source.Text;
        cm.Sourceurl = this.sourceurl.Text;
        if (this.articletype.Text == "3")
        {//文件
            cm.Linkpath = this.filedown.Value;
        }
        else
        {//链接
            cm.Linkpath = this.linkpath.Text;
        }
        cm.Author = this.author.Text;
        cm.Tag = this.tag.Value;

        if (this.expertid.Value != null && this.expertid.Value != "")
        {
            cm.ExpertId = int.Parse(this.expertid.Value);
        }

        cm.Columnid = int.Parse(Request.Params["columnid"]);

        cm.Addusername = this.GetSessionUsersModel().Username;
        cm.Modifyusername = this.GetSessionUsersModel().Username;
        if (aid > 0)
        {//修改
            this.PopedomValidate(Request.Params["layerid"], MOD);
            cm.Articleid = aid;
            CmsArticleBll cabll= new CmsArticleBll();
            bool r = cabll.Update(cm);
            if (r)
            {
                insertLog("修改文章:" + this.title.Text);
                if (this.state.Value == "1")
                {
                    cabll.Pubone(aid, GetSessionUsersModel().Username);
                    insertLog("发布文章:" + this.title.Text);
                }
            }
        }
        else
        {
            this.PopedomValidate(Request.Params["layerid"], ADD);
            if (new CmsArticleBll().Add(cm))
            {
                insertLog("新增文章:" + this.title.Text);
            }
        }
        Response.Redirect("info_list.aspx?columnid=" + Request.Params["columnid"] + "&layerid=" + Request.Params["layerid"], true);

    }

    protected void autotag_onclick(object sender, EventArgs e)
    {
        if (this.content.Text != "")
        {
            this.tagname.Text = "";
            this.tag.Value = "";
            CmsArticleTagModel q = new CmsArticleTagModel();
            q.State = 1;
            IList taglist = new CmsArticleTagBll().GetTagList(q);
            for (int i = 0; i < taglist.Count; i++)
            {
                CmsArticleTagModel catm = (CmsArticleTagModel)taglist[i];
                if (this.content.Text.IndexOf(catm.TagName) > -1)
                {
                    this.tag.Value += catm.TagId + ",";
                    this.tagname.Text += catm.TagName + ",";
                }
            }
        }
        
    }


    protected void articletype_SelectedIndexChanged(object sender, EventArgs e)
    {
        RadioButtonList rbl = (RadioButtonList)sender;
        this.down.Visible = false;
        this.link.Visible = false;
        this.con.Visible = false;
        if (rbl.Text == "3")
        {//文件
            this.down.Visible = true;
        }
        else if (rbl.Text == "2")
        {//链接
            this.link.Visible = true;

        }
        else
        {
            this.con.Visible = true;
        }
    }
    protected void expertlist_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (this.expertlist.SelectedItem.Text != "请选择")
            this.author.Text = this.expertlist.SelectedItem.Text;
        else
            this.author.Text = "";
        
        this.expertid.Value = this.expertlist.SelectedValue;
    }
}
